package haitao.dialog.view;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import haitao.library.dialog.R;

public class AlertDialog extends Dialog {

	public AlertDialog(Context context) {
		super(context);
	}

	public AlertDialog(Context context, int theme) {
		super(context, theme);
	}

	public static class Builder {

		private Context context; // 上下文对象
		private String title; // 对话框标题
		private String message; // 对话框内容
		private String confirm_btnText; // 按钮名称“确定”
		private String cancel_btnText; // 按钮名称“取消”
		private String neutral_btnText; // 按钮名称“隐藏”
		private View contentView; // 对话框中间加载的其他布局界面
		/* 按钮监听事件 */
		private DialogInterface.OnClickListener confirm_btnClickListener;
		private DialogInterface.OnClickListener cancel_btnClickListener;
		private DialogInterface.OnClickListener neutral_btnClickListener;


		public Builder setTitleSize(float titleSize) {
			this.titleSize = titleSize;
			return this;
		}

		public Builder setCancelSize(float cancelSize) {
			this.cancelSize = cancelSize;
			return this;
		}

		public Builder setConfirmSize(float confirmSize) {
			this.confirmSize = confirmSize;
			return this;
		}

		public Builder setContentSize(float contentSize) {
			this.contentSize = contentSize;
			return this;
		}

		public Builder setTitleColor(int titleColor) {
			this.titleColor = titleColor;
			return this;
		}

		public Builder setCancelColor(int cancelColor) {
			this.cancelColor = cancelColor;
			return this;
		}

		public Builder setConfirmColor(int confirmColor) {
			this.confirmColor = confirmColor;
			return this;
		}

		public Builder setContentColor(int contentColor) {
			this.contentColor = contentColor;
			return this;
		}
		public Builder setMsgGravity(int msgGravity) {
			this.msgGravity = msgGravity;
			return this;
		}

		public Builder setMinMsgHeight(int minMsgHeight) {
			this.minMsgHeight = minMsgHeight;
			return this;
		}

		private float titleSize;
		private float cancelSize;
		private float confirmSize;
		private float contentSize;
		private int titleColor;
		private int cancelColor;
		private int confirmColor;
		private int contentColor;

		private int minMsgHeight;


		private int msgGravity=-100;

		public Builder(Context context) {
			this.context = context;
		}

		/* 设置对话框信息 */
		public Builder setMessage(String message) {
			this.message = message;
			return this;
		}


		/**
		 * Set the Dialog message from resource
		 * @param message
		 * @return
		 */
		public Builder setMessage(int message) {
			this.message = (String) context.getText(message);
			return this;
		}

		/**
		 * Set the Dialog title from resource
		 *
		 * @param title
		 * @return
		 */
		public Builder setTitle(int title) {
			this.title = (String) context.getText(title);
			return this;
		}

		/**
		 * Set the Dialog title from String
		 *
		 * @param title
		 * @return
		 */
		public Builder setTitle(String title) {
			this.title = title;
			return this;
		}

		/**
		 * 设置对话框界面
		 *
		 * @param v
		 *            View
		 * @return
		 */
		public Builder setContentView(View v) {
			this.contentView = v;
			return this;
		}

		/**
		 * Set the positive button resource and it's listener
		 *
		 * @param confirm_btnText
		 * @return
		 */
		public Builder setPositiveButton(int confirm_btnText,
				DialogInterface.OnClickListener listener) {
			this.confirm_btnText = (String) context.getText(confirm_btnText);
			this.confirm_btnClickListener = listener;
			return this;
		}

		/**
		 * Set the positive button and it's listener
		 *
		 * @param confirm_btnText
		 * @return
		 */
		public Builder setPositiveButton(String confirm_btnText,
				DialogInterface.OnClickListener listener) {
			this.confirm_btnText = confirm_btnText;
			this.confirm_btnClickListener = listener;
			return this;
		}


		/**
		 * Set the negative button resource and it's listener
		 * @param cancel_btnText
		 * @param listener
		 * @return
		 */
		public Builder setNegativeButton(int cancel_btnText,
				DialogInterface.OnClickListener listener) {
			this.cancel_btnText = (String) context.getText(cancel_btnText);
			this.cancel_btnClickListener = listener;
			return this;
		}


		/**
		 * Set the negative button and it's listener
		 * @param cancel_btnText
		 * @param listener
		 * @return
		 */
		public Builder setNegativeButton(String cancel_btnText,
				DialogInterface.OnClickListener listener) {
			this.cancel_btnText = cancel_btnText;
			this.cancel_btnClickListener = listener;
			return this;
		}


		/**
		 * Set the netural button resource and it's listener
		 * @param neutral_btnText
		 * @param listener
		 * @return
		 */
		public Builder setNeutralButton(int neutral_btnText,
				DialogInterface.OnClickListener listener) {
			this.neutral_btnText = (String) context.getText(neutral_btnText);
			this.neutral_btnClickListener = listener;
			return this;
		}


		/**
		 *  Set the netural button and it's listener
		 * @param neutral_btnText
		 * @param listener
		 * @return
		 */
		public Builder setNeutralButton(String neutral_btnText,
				DialogInterface.OnClickListener listener) {
			this.neutral_btnText = neutral_btnText;
			this.neutral_btnClickListener = listener;
			return this;
		}

		public AlertDialog create() {
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			// instantiate the dialog with the custom Theme
			final AlertDialog dialog = new AlertDialog(context,
					R.style.mystyle);
			View layout = inflater.inflate(R.layout.dialog_ios, null);
			dialog.addContentView(layout, new LayoutParams(
					LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
			// set the dialog title
			((TextView) layout.findViewById(R.id.title)).setText(title);
			((TextView) layout.findViewById(R.id.title)).getPaint()
					.setFakeBoldText(true);

			//notitle
			if (title == null || title.trim().length() == 0) {
				((TextView) layout.findViewById(R.id.message))
						.setGravity(Gravity.CENTER);
				((TextView) layout.findViewById(R.id.title)).setVisibility(View.GONE);

				if (contentColor!=0){
					((TextView) layout.findViewById(R.id.message)).setTextColor(contentColor);
				}if (contentSize!=0){
					((TextView) layout.findViewById(R.id.message)).setTextSize(contentSize);
				}

			}
			// set the content message
			if (message != null) {
				((TextView) layout.findViewById(R.id.message)).setText(message);
				if (minMsgHeight!=0){
					layout.findViewById(R.id.message_ll).getLayoutParams().height=dp2px(context,minMsgHeight);
				}
				if (msgGravity!=-100)
				((TextView) layout.findViewById(R.id.message)).setGravity(msgGravity);
			} else{
				((TextView) layout.findViewById(R.id.message)).setVisibility(View.GONE);
				layout.findViewById(R.id.message_ll).getLayoutParams().height=dp2px(context,40);

			} if (contentView != null) {
				// if no message set
				// add the contentView to the dialog body
				((LinearLayout) layout.findViewById(R.id.message_ll)).removeAllViews();
				((LinearLayout) layout.findViewById(R.id.message_ll)).addView(
						contentView, new LayoutParams(
								LayoutParams.WRAP_CONTENT,
								LayoutParams.WRAP_CONTENT));

			}
			if (neutral_btnText != null && confirm_btnText != null
					&& cancel_btnText != null) {
				((Button) layout.findViewById(R.id.confirm_btn))
						.setText(confirm_btnText);
				if (neutral_btnClickListener != null) {
					((Button) layout.findViewById(R.id.neutral_btn))
							.setOnClickListener(new View.OnClickListener() {
								public void onClick(View v) {
									neutral_btnClickListener.onClick(dialog,
											DialogInterface.BUTTON_NEUTRAL);
									dialog.dismiss();
								}
							});
				} else {
					((Button) layout.findViewById(R.id.neutral_btn))
							.setOnClickListener(new View.OnClickListener() {

								@Override
								public void onClick(View v) {
									dialog.dismiss();
								}
							});
				}
			} else {
				// if no confirm button or cancle button or neutral just set the
				// visibility to GONE
				layout.findViewById(R.id.neutral_btn).setVisibility(View.GONE);
				layout.findViewById(R.id.single_line).setVisibility(View.GONE);
			}
			// set the confirm button
			if (confirm_btnText != null) {
				((Button) layout.findViewById(R.id.confirm_btn))
						.setText(confirm_btnText);
				if (confirm_btnClickListener != null) {
					((Button) layout.findViewById(R.id.confirm_btn))
							.setOnClickListener(new View.OnClickListener() {
								public void onClick(View v) {
									confirm_btnClickListener.onClick(dialog,
											DialogInterface.BUTTON_POSITIVE);
									dialog.dismiss();
								}
							});
				} else {
					((Button) layout.findViewById(R.id.confirm_btn))
							.setOnClickListener(new View.OnClickListener() {

								@Override
								public void onClick(View v) {
									dialog.dismiss();
								}
							});
				}
			} else {
				// if no confirm button just set the visibility to GONE
				layout.findViewById(R.id.confirm_btn).setVisibility(View.GONE);
				layout.findViewById(R.id.second_line).setVisibility(View.GONE);
				layout.findViewById(R.id.cancel_btn).setBackgroundResource(
						R.drawable.ios_single_btn_select);
			}
			// set the cancel button
			if (cancel_btnText != null) {
				((Button) layout.findViewById(R.id.cancel_btn))
						.setText(cancel_btnText);
				if (cancel_btnClickListener != null) {
					((Button) layout.findViewById(R.id.cancel_btn))
							.setOnClickListener(new View.OnClickListener() {
								public void onClick(View v) {
									cancel_btnClickListener.onClick(dialog,
											DialogInterface.BUTTON_NEGATIVE);
									dialog.dismiss();
								}
							});
				} else {
					((Button) layout.findViewById(R.id.cancel_btn))
							.setOnClickListener(new View.OnClickListener() {

								@Override
								public void onClick(View v) {
									dialog.dismiss();
								}
							});
				}
			} else {
				// if no cancel button just set the visibility to GONE
				layout.findViewById(R.id.cancel_btn).setVisibility(View.GONE);
				layout.findViewById(R.id.second_line).setVisibility(View.GONE);
				layout.findViewById(R.id.confirm_btn).setBackgroundResource(
						R.drawable.ios_single_btn_select);
			}

			//DisplayMetrics dm = context.getResources().getDisplayMetrics();
			// 
			if (titleColor!=0){
				((TextView) layout.findViewById(R.id.title)).setTextColor(titleColor);
			}if (titleSize!=0){
				((TextView) layout.findViewById(R.id.title)).setTextSize(titleSize);
			}

			if (confirmColor!=0){
				((Button) layout.findViewById(R.id.confirm_btn)).setTextColor(confirmColor);
			}if (confirmSize!=0){
				((Button) layout.findViewById(R.id.confirm_btn)).setTextSize(confirmSize);
			}

			if (cancelColor!=0){
				((Button) layout.findViewById(R.id.cancel_btn)).setTextColor(cancelColor);
			}if (cancelSize!=0){
				((Button) layout.findViewById(R.id.cancel_btn)).setTextSize(cancelSize);
			}

			float widthScreen = getScreenWidth(context);
			int width = dp2px(context, 240);
			if (widthScreen*4/5<width) {
				width =(int) widthScreen*4/5;
			}
			dialog.setContentView(layout, new LayoutParams(width, LayoutParams.WRAP_CONTENT));
			return dialog;
		}
		public  int getScreenWidth(Context activity) {
			new DisplayMetrics();
			DisplayMetrics dm = activity.getResources().getDisplayMetrics();
			return dm.widthPixels;
		}
		public int dp2px(Context context, float value) {
			float scale = (float)context.getResources().getDisplayMetrics().densityDpi;
			return (int)(value * (scale / 160.0F) + 0.5F);
		}

	}
}
